package org.codefinger.dao.impl;

import java.util.Iterator;
import java.util.Map.Entry;

import org.codefinger.dao.impl.sql.ast.TextExpression;
import org.codefinger.dao.util.Lang;

public class AllColumnCache implements ColumnCache {

	public AllColumnCache() {
		super();
	}

	@Override
	public void resolveColumn(QueryBuilder builder) {
		Iterator<String> iterator = builder.subColumnsAlias;
		String tableAlias;
		String name;
		if (iterator != null) {
			tableAlias = builder.subQueryAlias;
			while (iterator.hasNext()) {
				name = iterator.next();
				builder.addColumnItem(new TextExpression(//
						Lang.joinStr(tableAlias.length() + 1 + name.length(), tableAlias, ".", name)), name, name);
			}
		}
		for (Entry<String, String> entry : builder.tableAliasMap.entrySet()) {
			iterator = builder.getAllColumns(entry.getValue());
			tableAlias = entry.getKey();
			while (iterator.hasNext()) {
				name = iterator.next();
				builder.addColumnItem(new TextExpression(//
						Lang.joinStr(tableAlias.length() + 1 + name.length(), tableAlias, ".", name)), name, name);
			}
		}
	}
}
